package com.dingreading.cloud.app.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.facebody.model.v20191230.*;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 阿里云人脸识别工具类
 */
@RefreshScope
@Component
public class FaceDetectUtil {

    @Value("${aliYunFaceDb}")
    private String faceDb;

    public static FaceDetectUtil util;

    @PostConstruct
    public void init() {
        util = this;
    }

    // DefaultProfile.getProfile的参数分别是地域，access_key_id, access_key_secret
//    public static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI5tBbhjmR1ff1EHLQBgDf", "P09ItbJF4IuMu3cIOQCFH5S3PiK33x");
    public static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI5tJmDwmtXtgCAFiMaCtL", "aoUeYXkNhzm3sLKaNgCByAzjyR7yjT");

    /**
     * 添加人脸样本
     *
     * @param entityId 唯一标识
     * @return AddFaceEntityResponse
     * @throws ClientException
     */
    public static AddFaceEntityResponse addFaceEntity(String entityId) throws ClientException {
        IAcsClient client = new DefaultAcsClient(profile);
        AddFaceEntityRequest request = new AddFaceEntityRequest();
        request.setDbName(util.faceDb);
        request.setEntityId(entityId);

        AddFaceEntityResponse response = client.getAcsResponse(request);
//        System.out.println(new Gson().toJson(response));
        return response;
    }

    /**
     * 添加人脸数据
     *
     * @param entityId 唯一标识
     * @param imageUrl 人脸图片
     * @return AddFaceResponse
     * @throws ClientException
     */
    public static AddFaceResponse addFace(String entityId, String imageUrl) throws ClientException {
        IAcsClient client = new DefaultAcsClient(profile);
        AddFaceRequest request = new AddFaceRequest();
        request.setDbName(util.faceDb);
        request.setImageUrl(imageUrl);
        request.setEntityId(entityId);
        request.setQualityScoreThreshold(50f);

        AddFaceResponse response = client.getAcsResponse(request);
        return response;
    }

    /**
     * 删除人脸样本
     *
     * @param entityId 唯一标识
     * @return DeleteFaceEntityResponse
     * @throws ClientException
     */
    public static DeleteFaceEntityResponse deleteFaceEntity(String entityId) throws ClientException {
        IAcsClient client = new DefaultAcsClient(profile);
        DeleteFaceEntityRequest request = new DeleteFaceEntityRequest();
        request.setDbName(util.faceDb);
        request.setEntityId(entityId);

        DeleteFaceEntityResponse response = client.getAcsResponse(request);
        return response;
    }

    /**
     * 删除人脸数据
     *
     * @param faceId 人脸数据id
     * @return DeleteFaceResponse
     * @throws ClientException
     */
    public static DeleteFaceResponse deleteFace(String faceId) throws ClientException {
        IAcsClient client = new DefaultAcsClient(profile);
        DeleteFaceRequest request = new DeleteFaceRequest();
        request.setDbName(util.faceDb);
        request.setFaceId(faceId);

        DeleteFaceResponse response = client.getAcsResponse(request);
        return response;
    }

    /**
     * 根据人脸唯一标识获取人脸样本
     *
     * @param entityId 人脸唯一标识
     * @return GetFaceEntityResponse
     * @throws ClientException
     */
    public static GetFaceEntityResponse getFaceEntityByEntityId(String entityId) throws ClientException {
        IAcsClient client = new DefaultAcsClient(profile);
        GetFaceEntityRequest request = new GetFaceEntityRequest();
        request.setDbName(util.faceDb);
        request.setEntityId(entityId);

        GetFaceEntityResponse response = client.getAcsResponse(request);
        return response;
    }

    /**
     * 搜索人脸
     *
     * @param imageUrl 输入人脸图片
     * @return SearchFaceResponse
     * @throws ClientException
     */
    public static SearchFaceResponse searchFaceEntity(String imageUrl) throws ClientException {
        IAcsClient client = new DefaultAcsClient(profile);
        SearchFaceRequest request = new SearchFaceRequest();
        request.setDbName(util.faceDb);
//        request.setDbName("default");
        request.setImageUrl(imageUrl);
        request.setLimit(5);
        request.setQualityScoreThreshold(60f);
        request.setMaxFaceNum(1L);

        SearchFaceResponse response = client.getAcsResponse(request);
        return response;
    }

    /**
     * （请勿随意使用）
     * 创建人脸库
     *
     * @param faceDbName 库名
     * @return CreateFaceDbResponse
     * @throws ClientException
     */
    public static CreateFaceDbResponse createFaceDb(String faceDbName) throws ClientException {
        IAcsClient client = new DefaultAcsClient(profile);
        CreateFaceDbRequest request = new CreateFaceDbRequest();
        request.setName(faceDbName);

        CreateFaceDbResponse response = client.getAcsResponse(request);
        return response;
    }

}
